<template>
  <div class="page-search">
    <Lczform v-bind="searchFormConfig" v-model="formData">
      <template #header>
        <h1 class="header">高级搜索</h1>
      </template>
      <template #footer>
        <div class="handle-btns">
          <el-button size="medium" class="el-icon-refresh" @click="handleReset"
            >重置</el-button
          >
          <el-button
            size="medium"
            type="primary"
            class="el-icon-search"
            @click="handleSearch"
            >搜索</el-button
          >
        </div>
      </template>
    </Lczform>
  </div>
</template>

<script lang="ts">
import Lczform from '@/base-ui/form'
import { defineComponent, ref } from 'vue'

export default defineComponent({
  components: {
    Lczform
  },
  props: {
    searchFormConfig: {
      type: Object,
      required: true
    }
  },
  emits: ['handleSearch', 'handleReset'],
  setup(props, { emit }) {
    const formItemConfig = props.searchFormConfig?.formItems ?? []
    const formDataObj: any = {}
    for (const item of formItemConfig) {
      formDataObj[item.field] = ''
    }
    const formData = ref(formDataObj)
    // 搜索
    const handleSearch = () => {
      emit('handleSearch', formData.value)
    }
    // 重置
    const handleReset = () => {
      for (const item in formDataObj) {
        formData.value[item] = formDataObj[item]
      }
      emit('handleReset')
    }
    return {
      handleSearch,
      handleReset,
      formData
    }
  }
})
</script>

<style scoped></style>
